package com.lab.jumper;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import com.lab.app.BaseFragment;
import com.lab.logtrack.AppLog;

import java.util.ConcurrentModificationException;

/**
 * Created by ak
 * Fragment操作跳转类
 */
public class FragmentJumper {

    public static final void addFragment(FragmentManager fm, int containerViewId, Fragment fragment, String tag, boolean addToBackStack) {
        try {
            Fragment preFragment = fm.findFragmentById(containerViewId);
            try {
                if (preFragment != null && preFragment instanceof BaseFragment) {
                    fragment.setTargetFragment(preFragment, -1);
                }
            } catch (ConcurrentModificationException e) {
                AppLog.e(e.getMessage());
            }
            FragmentTransaction ft = fm.beginTransaction();
            ft.add(containerViewId, fragment, tag);
            if (addToBackStack) ft.addToBackStack(tag);
            ft.commitAllowingStateLoss();
        } catch (Exception e) {
            AppLog.w(e.getMessage());
        }
    }

    public static final void addFragment(FragmentManager fm, int containerViewId, Fragment fragment, String tag, boolean addToBackStack, int animEnter, int animExit, int animPopEnter, int animPopExit) {
        try {
            //TODO animation
            /*if (!SettingUtil.isSupportAnimationSetting()) {
                animEnter = animExit = animPopEnter = animPopExit = 0;
            } else {
                if (animEnter < 0) animEnter = 0;
                if (animExit < 0) animExit = 0;
                if (animPopEnter < 0) animPopEnter = 0;
                if (animPopExit < 0) animPopExit = 0;
            }*/
            /*Fragment preFragment = fm.findFragmentById(containerViewId);
            try {
                if (preFragment != null && preFragment instanceof BaseFragment) {
                    ((BaseFragment) preFragment).tryToReleaseImageLoader();
                    fragment.setTargetFragment(preFragment, -1);
                    if (preFragment instanceof SwipeBackFragmentBase) {
                        ((SwipeBackFragmentBase) preFragment).hideDisableSwipe(true);
                    }
                }
            } catch (ConcurrentModificationException e) {
                AppLog.e(e.getMessage());
            }*/
            FragmentTransaction ft = fm.beginTransaction();
            ft.setCustomAnimations(animEnter, animExit, animPopEnter, animPopExit);
            ft.add(containerViewId, fragment, tag);
            if (addToBackStack) ft.addToBackStack(tag);
            ft.commitAllowingStateLoss();
        } catch (Exception e) {
            AppLog.w(e.getMessage());
        }
    }

    public static final void addFragment(FragmentManager fm, Context context, int containerViewId, Class<? extends Fragment> fragmentClass, Bundle args, String tag, boolean addToBackStack) {
        try {
            Fragment fragment = Fragment.instantiate(context, fragmentClass.getName(), args);
            addFragment(fm, containerViewId, fragment, tag, addToBackStack);
        } catch (Exception e) {
            AppLog.w(e.getMessage());
        }
    }

    public static final void addFragments(FragmentManager fm, int containerViewId, boolean addToBackStack, Fragment... fragments) {
        try {
            FragmentTransaction ft = fm.beginTransaction();
            for (Fragment fragment : fragments) {
                if (null != fragment)
                    ft.add(containerViewId, fragment);
            }
            if (addToBackStack) ft.addToBackStack(null);
            ft.commitAllowingStateLoss();
        } catch (Exception e) {
            AppLog.w(e.getMessage());
        }
    }

    public static final void replaceFragment(FragmentManager fm, int containerViewId, Fragment fragment, String tag, boolean addToBackStack) {
        try {
            FragmentTransaction ft = fm.beginTransaction();
            removeAllFragments(fm, false);
            ft.replace(containerViewId, fragment, tag);
            if (addToBackStack) ft.addToBackStack(tag);
            ft.commitAllowingStateLoss();
        } catch (Exception e) {
            AppLog.w(e.getMessage());
        }
    }

    public static final void replaceFragment(FragmentManager fm, int containerViewId, Fragment fragment, String tag, boolean addToBackStack, int animEnter, int animExit, int animPopEnter, int animPopExit) {
        try {
            /*if (!SettingUtil.isSupportAnimationSetting()) {
                animEnter = animExit = animPopEnter = animPopExit = 0;
            } else {
                if (animEnter < 0) animEnter = 0;
                if (animExit < 0) animExit = 0;
                if (animPopEnter < 0) animPopEnter = 0;
                if (animPopExit < 0) animPopExit = 0;
            }*/
            FragmentTransaction ft = fm.beginTransaction();
            removeAllFragments(fm, false);
            ft.setCustomAnimations(animEnter, animExit, animPopEnter, animPopExit);
            ft.replace(containerViewId, fragment, tag);
            if (addToBackStack) ft.addToBackStack(tag);
            ft.commitAllowingStateLoss();
        } catch (Exception e) {
            AppLog.w(e.getMessage());
        }
    }

    public static final void replaceFragment(FragmentManager fm, Context context, int containerViewId, Class<? extends Fragment> fragmentClass, Bundle args, String tag, boolean addToBackStack) {
        try {
            Fragment fragment = Fragment.instantiate(context, fragmentClass.getName(), args);
            replaceFragment(fm, containerViewId, fragment, tag, addToBackStack);
        } catch (Exception e) {
            AppLog.w(e.getMessage());
        }
    }

    public static final void removeFragment(FragmentManager fm, Fragment fragment, boolean addToBackStack) {
        try {
            FragmentTransaction ft = fm.beginTransaction();
            ft.remove(fragment);
            if (addToBackStack) ft.addToBackStack(null);
            ft.commitAllowingStateLoss();
        } catch (Exception e) {
            AppLog.w(e.getMessage());
        }
    }

    public static final void removeFragments(FragmentManager fm, boolean addToBackStack, Fragment... fragments) {
        try {
            FragmentTransaction ft = fm.beginTransaction();
            for (Fragment fragment : fragments) {
                if (null != fragment)
                    ft.remove(fragment);
            }
            if (addToBackStack) ft.addToBackStack(null);
            ft.commitAllowingStateLoss();
        } catch (Exception e) {
            AppLog.w(e.getMessage());
        }
    }

    public static final void removeAllFragments(FragmentManager fm, boolean addToBackStack) {
        try {
            FragmentTransaction ft = fm.beginTransaction();
            for (Fragment fragment : fm.getFragments()) {
                if (null != fragment)
                    ft.remove(fragment);
            }
            if (addToBackStack) ft.addToBackStack(null);
            ft.commitAllowingStateLoss();
        } catch (Exception e) {
            AppLog.w(e.getMessage());
        }
    }

    public static final void hideFragment(FragmentManager fm, Fragment fragment, boolean addToBackStack) {
        try {
            FragmentTransaction ft = fm.beginTransaction();
            ft.hide(fragment);
            if (addToBackStack) ft.addToBackStack(null);
            ft.commitAllowingStateLoss();
        } catch (Exception e) {
            AppLog.w(e.getMessage());
        }
    }

    public static final void hideFragments(FragmentManager fm, boolean addToBackStack, Fragment... fragments) {
        try {
            FragmentTransaction ft = fm.beginTransaction();
            for (Fragment fragment : fragments) {
                if (null != fragment)
                    ft.hide(fragment);
            }
            if (addToBackStack) ft.addToBackStack(null);
            ft.commitAllowingStateLoss();
        } catch (Exception e) {
            AppLog.w(e.getMessage());
        }
    }

    public static final void showFragment(FragmentManager fm, Fragment fragment, boolean addToBackStack) {
        try {
            FragmentTransaction ft = fm.beginTransaction();
            ft.show(fragment);
            if (addToBackStack) ft.addToBackStack(null);
            ft.commitAllowingStateLoss();
        } catch (Exception e) {
            AppLog.w(e.getMessage());
        }
    }

    public static final void showFragments(FragmentManager fm, boolean addToBackStack, Fragment... fragments) {
        try {
            FragmentTransaction ft = fm.beginTransaction();
            for (Fragment fragment : fragments) {
                if (null != fragment)
                    ft.show(fragment);
            }
            if (addToBackStack) ft.addToBackStack(null);
            ft.commitAllowingStateLoss();
        } catch (Exception e) {
            AppLog.w(e.getMessage());
        }
    }

    public static final void openFragment(FragmentManager fm, int containerViewId, Fragment oldFragment, Fragment newFragment, boolean addToBackStack) {
        try {
            FragmentTransaction ft = fm.beginTransaction();
            ft.hide(oldFragment);
            ft.add(containerViewId, newFragment);
            if (addToBackStack) ft.addToBackStack(null);
            ft.commitAllowingStateLoss();
        } catch (Exception e) {
            AppLog.w(e.getMessage());
        }
    }

    public static final void openFragment(FragmentManager fm, int containerViewId, Fragment oldFragment, Fragment newFragment, boolean addToBackStack, int animEnter, int animExit, int animPopEnter, int animPopExit) {
        try {
            /*if (!SettingUtil.isSupportAnimationSetting()) {
                animEnter = animExit = animPopEnter = animPopExit = 0;
            } else {
                if (animEnter < 0) animEnter = 0;
                if (animExit < 0) animExit = 0;
                if (animPopEnter < 0) animPopEnter = 0;
                if (animPopExit < 0) animPopExit = 0;
            }*/
            FragmentTransaction ft = fm.beginTransaction();
            ft.hide(oldFragment);
            ft.add(containerViewId, newFragment);
            ft.setCustomAnimations(animEnter, animExit, animPopEnter, animPopExit);
            if (addToBackStack) ft.addToBackStack(null);
            ft.commitAllowingStateLoss();
        } catch (Exception e) {
            AppLog.w(e.getMessage());
        }
    }

    public static final Fragment findFragmentById(FragmentManager fm, int id) {
        try {
            return fm.findFragmentById(id);
        } catch (Exception e) {
            AppLog.w(e.getMessage());
        }
        return null;
    }

    public static final Fragment findFragmentByTag(FragmentManager fm, String tag) {
        try {
            return fm.findFragmentByTag(tag);
        } catch (Exception e) {
            AppLog.w(e.getMessage());
        }
        return null;
    }

//    public static final void showFragmentDialog(FragmentManager fm, DialogFragment dialogFragment, String tag) {
//        try {
//            dialogFragment.setShowsDialog(true);
//            dialogFragment.show(fm, tag);
//        } catch (Exception e) {
//            AppLog.w(e.getMessage());
//        }
//    }
//
//    public static final void dismissFragmentDialog(DialogFragment dialogFragment) {
//        try {
//            dialogFragment.dismissAllowingStateLoss();
//        } catch (Exception e) {
//            AppLog.w(e.getMessage());
//        }
//    }

}
